package controller

import (
	"book/internal/logic"
	"book/internal/pb"
	"context"
)

type Book struct {
	pb.UnimplementedBookServer
	BookLogic *logic.BookLogic
}

// Favourite 收藏漫画
func (b *Book) Favourite(ctx context.Context, req *pb.FavouriteRequest) (*pb.FavouriteResponse, error) {
	operate, err := b.BookLogic.Favourite(ctx, req.ComicId)
	if err != nil {
		return nil, err
	}
	return &pb.FavouriteResponse{Operate: operate}, nil
}

// Index 收藏的漫画列表
func (b *Book) Index(ctx context.Context, req *pb.IndexRequest) (*pb.IndexResponse, error) {
	b.BookLogic.SetPerPage(req.PerPage)
	b.BookLogic.SetPage(req.Page)
	return &pb.IndexResponse{
		List: b.BookLogic.List(ctx),
		Page: &pb.Page{
			CurrentPage: b.BookLogic.GetCurrentPage(),
			NextPage:    b.BookLogic.GetNextPage(),
			Total:       b.BookLogic.GetTotal(),
			PerPage:     b.BookLogic.GetPerPage(),
		},
	}, nil
}

func (b *Book) IsFavourite(ctx context.Context, req *pb.IsFavouriteRequest) (*pb.IsFavouriteResponse, error) {
	isFavourite, err := b.BookLogic.IsFavourite(ctx, req.ComicId)
	if err != nil {
		return nil, err
	}
	return &pb.IsFavouriteResponse{
		IsFavourite: isFavourite,
	}, nil
}
